package com.pac.role.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.role.bean.RoleBean;
import com.pac.role.bean.RoleMenuBean;
import com.pac.role.dao.RoleDao;
import com.pac.role.service.RoleMenuService;
import com.pac.role.service.RoleService;

/**
 * 角色Service实现
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月14日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "roleService")
public class RoleServiceImpl implements RoleService {

	private static Logger logger = Logger.getLogger(RoleServiceImpl.class);

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private RoleMenuService roleMenuService;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public String addRole(RoleBean roleBean) {
		if (null == roleBean) {
			logger.error("添加角色出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(roleBean.getRoleName())) {
			logger.error("添加角色出错，角色名称为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "角色名称为空");
		}

		String roleId = roleDao.addRole(roleBean);

		// 角色绑定菜单处理
		String menuIds = roleBean.getMenuIds();
		String menuIdArr[] = menuIds.split(",");
		for (String menuId : menuIdArr) {
			RoleMenuBean roleMenuBean = new RoleMenuBean();
			roleMenuBean.setMenuId(menuId);
			roleMenuBean.setRoleId(roleId);

			roleMenuService.addRoleMenu(roleMenuBean);
		}

		return roleId;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateRole(RoleBean roleBean) {
		if (null == roleBean) {
			logger.error("修改角色出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(roleBean.getId())) {
			logger.error("修改角色出错，角色ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "角色ID为空");
		}
		// 修改角色
		boolean updateRole = roleDao.updateRole(roleBean);
		String menuIds = roleBean.getMenuIds();
		String[] menuId = menuIds.split(",");
		// 删除角色菜单关系
		boolean delRole = roleMenuService.delRoleMenuByRoleId(roleBean.getId());
		// 添加角色菜单关系
		for (String m : menuId) {
			RoleMenuBean rm = new RoleMenuBean();
			rm.setMenuId(m);
			rm.setRoleId(roleBean.getId());
			roleMenuService.addRoleMenu(rm);
		}
		if (updateRole && delRole) {
			return true;
		}
		return false;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delRoleById(String roleId) {
		if (StringTools.isNull(roleId)) {
			logger.error("删除角色出错，角色ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "角色ID为空");
		}
		
		//删除角色关联的菜单
		roleMenuService.delRoleMenuByRoleId(roleId);
		
		RoleBean roleBean = new RoleBean();
		roleBean.setId(roleId);
		return roleDao.delRole(roleBean);
	}

	@Override
	public List<RoleBean> queryRole(RoleBean roleBean) {
		List<RoleBean> roleBeanList = roleDao.queryRole(roleBean);
		if (null != roleBeanList) {
			// 获取角色绑定的菜单ID，多个以,分割
			for (RoleBean roleBeanT : roleBeanList) {
				RoleMenuBean roleMenuBeanQ = new RoleMenuBean();
				roleMenuBeanQ.setRoleId(roleBeanT.getId());
				List<RoleMenuBean> roleMenuBeanList = roleMenuService
						.queryRoleMenu(roleMenuBeanQ);
				String menuIds = "";
				for (RoleMenuBean roleMenuBean : roleMenuBeanList) {
					if (!StringTools.isNull(menuIds)) {
						menuIds += ",";
					}
					menuIds += roleMenuBean.getMenuId();
				}
				roleBeanT.setMenuIds(menuIds);
			}
		}

		return roleBeanList;
	}

	@Override
	public RoleBean findRole(RoleBean roleBean) {
		List<RoleBean> roleList = roleDao.queryRole(roleBean);
		if (null != roleList && roleList.size() > 0) {
			return roleList.get(0);
		}
		return null;
	}

	@Override
	public List<RoleBean> queryRoleAll(RoleBean roleBean) {
		return roleDao.queryRole(roleBean);
	}

}
